

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_color_tool.dart';

class FMDetailBaseCellWidget extends StatefulWidget {

  String title;

  String content;

  String placeHolder;

  TextInputType? inputType;

  bool isClicked;

  GestureTapCallback? onTap;

  Function(String)? onChanged;

  FMDetailBaseCellWidget({
    super.key,
    required this.title,
    required this.content,
    required this.placeHolder,
    this.inputType = TextInputType.text,
    this.isClicked = false,
    this.onTap,
    this.onChanged
  });

  @override
  State<FMDetailBaseCellWidget> createState() => _FMDetailBaseCellWidgetState();
}

class _FMDetailBaseCellWidgetState extends State<FMDetailBaseCellWidget> {

  TextEditingController textEditingController = TextEditingController();

  @override
  void initState() {
    super.initState();
    textEditingController.text = widget.content;
  }

  @override
  void didUpdateWidget(covariant FMDetailBaseCellWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.content != oldWidget.content) {
      textEditingController.text = widget.content;
    }
  }

  // @override
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _topWidget(),
        const SizedBox(height: 10),
        _contentWidget(),
      ],
    );
  }

  Widget _topWidget() {
    return Text(
      widget.title,
      style: TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.w500,
        color: FMColorTool.blackColor,
      ),
    );
  }

  Widget _contentWidget() {
    return Container(
      height: 54,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: FMColorTool.color("#D1FFCA"),
      ),
      child: Stack(
        children: [
          _textFieldWidget(),
          _iconWidget(),
          _gestureWidget()
        ],
      ),
    );
  }

  Widget _textFieldWidget() {
    return Positioned(
      top: 4,
      bottom: 0,
      left: 16,
      right: 0,
      child: TextField(
          controller: textEditingController,
          keyboardType: widget.inputType,
          style: TextStyle(
            fontWeight: FontWeight.w500,
            fontSize: 14,
            color: FMColorTool.blackColor
          ),
          decoration: InputDecoration(
            contentPadding: const EdgeInsets.symmetric(vertical:0),
            hintText: widget.placeHolder,
            hintStyle: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: 14,
                color: FMColorTool.color("#333333").withOpacity(0.6)
            ),
            border: const OutlineInputBorder(
                borderSide: BorderSide.none
            ),
            filled: false,
          ),
        onChanged:widget.onChanged,
      ),
    );
  }

  Widget _iconWidget() {
    return Positioned(
      top: 0,
      bottom: 0,
      right: 14,
      child: widget.isClicked ? Image.asset(
        "assets/detail/detail_select_icon.png",
        width: 16,
        height: 16,
      ) : Container(),
    );
  }

  Widget _gestureWidget() {
    return widget.isClicked ?  Positioned(
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
      child: GestureDetector(
        onTap: (){
          widget.onTap!();
        },
      ),
    ) : Container();
  }
}
